package com.mybatis.generator.codegen;

import com.mybatis.generator.config.CodeGeneratorConfig;
import com.mybatis.generator.utils.FileUtil;
import com.mybatis.generator.utils.GenUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.IOException;
import java.util.Map;

public class CodeGeneratorDao {

    private CodeGeneratorConfig config;
    private String className;
    private Configuration cfg;

    public CodeGeneratorDao(Configuration cfg, CodeGeneratorConfig config, String className) {
        this.config = config;
        this.className = className;
        this.cfg = cfg;
    }

    public void build(Map<String, Object> variables, boolean isOverwrite) throws IOException {
        variables.put("packageEntity", config.getClassPackageEntity());
        variables.put("packageDao", config.getClassPackageDao());
        variables.put("packageDaoImpl", config.getClassPackageDaoImpl());

        Template templateEntity = cfg.getTemplate(config.getTemplateFileNameEntity());
        FileUtil.buildTemplate(variables, config.getClassPathEntity(), config.getClassNameEntityPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameEntitySuffix() + ".java", templateEntity, isOverwrite);

        Template templateMapper = cfg.getTemplate(config.getTemplateFileNameMapper());
        FileUtil.buildTemplate(variables, config.getClassPathMapper(), config.getClassNameMapperPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameMapperSuffix() + ".xml", templateMapper, isOverwrite);

        Template templateDao = cfg.getTemplate(config.getTemplateFileNameDao());
        FileUtil.buildTemplate(variables, config.getClassPathDao(), config.getClassNameDaoPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameDaoSuffix() + ".java", templateDao, isOverwrite);

        Template templateDaoImpl = cfg.getTemplate(config.getTemplateFileNameDaoImpl());
        FileUtil.buildTemplate(variables, config.getClassPathDaoImpl(), config.getClassNameDaoImplPrefix() + GenUtil.firstCharacterToUpper(className) + config.getClassNameDaoImplSuffix() + ".java", templateDaoImpl, isOverwrite);
}
    
}
